<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="${daoPackage}.${className_d}IDao">
	<!-- 封装${className_d}结果结合 -->
	<resultMap id="get-${className_d}-list" type="${className_d}">
		<#list tableCarrays as tableCarrays>
	    	<result property="${tableCarrays.carrayName_x}" column="${tableCarrays.carrayName_x}"/>
	    </#list>
	</resultMap>
	
	<!-- sql公用的代码块 -->
	<sql id="comListQuery${className_d}">
		<#list tableCarrays as tableCarrays>
			<#if tableCarrays.carrayName_x != primaryKeyCode >
			    <if test="${tableCarrays.carrayName_x} != null" >
	             	and ${tableCarrays.carrayName_x} = ${r'#{'}${tableCarrays.carrayName_x}${r'}'}
	            </if>
			</#if>
		</#list>
	</sql>
	
	<!-- 根据实体类查询 -->
	<select id="get${className_d}ByCondition" resultMap="get-${className_d}-list" parameterType="${className_d}">
		select * from ${className} 
		<where>
		 	<if test="${primaryKeyCode} != null">
		 		${primaryKeyCode} = ${r'#{'}${primaryKeyCode}${r'}'}
		 	</if>
		 	<include refid="comListQuery${className_d}"/>
			<if test="otherCondStr != null">
				and ${r'#{'}otherCondStr${r'}'}
			</if>
		</where>
		<if test="orderBy != null">
			Order by ${r'#{'}orderBy${r'}'}
		</if>
		;
	</select>
	<!-- 根据主键查询 -->
	<select id="get${className_d}ById" resultMap="get-${className_d}-list" parameterType="java.lang.String">
		select * from ${className} 
		<where>
			${primaryKeyCode} = ${r'#{'}id${r'}'};
		</where>
	</select>
	
	<!-- 分页查询 -->
	<select id="get${className_d}ByByPaging" parameterType="${className_d}" resultMap="get-${className_d}-list">
		select * from ${className}
		<where>
			<if test="${primaryKeyCode} != null">
		 		${primaryKeyCode} = ${r'#{'}${primaryKeyCode}${r'}'}
		 	</if>
			<include refid="comListQuery${className_d}"/>
			<if test="otherCondStr != null">
				and ${r'#{'}otherCondStr${r'}'}
			</if>
		</where>
		<if test="orderBy != null">
			Order by  ${r'#{'}orderBy${r'}'}
		</if>
		limit ${r'#{'}startPos${r'}'},${r'#{'}pageSize${r'}'}
		;
	</select>
	
	<!-- 条件查询记录数量 -->
	<select id="getCount${className_d}" parameterType="${className_d}" resultType="Integer">
		select count(0) from ${className}
		<where>
			<if test="${primaryKeyCode} != null">
		 		${primaryKeyCode} = ${r'#{'}${primaryKeyCode}${r'}'}
		 	</if>
			<include refid="comListQuery${className_d}"/>
		</where>
		;
	</select>
	
	<!-- 插入数据 -->
	<insert id="insert${className_d}" parameterType="${className_d}" useGeneratedKeys="true" keyProperty="${primaryKeyCode}">
		insert into ${className}
	    (
	    ${primaryKeyCode}
			<#list tableCarrays as tableCarrays>
				<#if tableCarrays.carrayName_x != primaryKeyCode>
					<if test="${tableCarrays.carrayName_x} != null" >
				    	, ${tableCarrays.carrayName_x}
				    </if>
				</#if>
			</#list>
	    )
	    values
	    (
    	DEFAULT
	    	<#list tableCarrays as tableCarrays>
				<#if tableCarrays.carrayName_x != primaryKeyCode>
					<if test="${tableCarrays.carrayName_x} != null">
						, ${r'#{'}${tableCarrays.carrayName_x}${r'}'}
					</if>
				</#if>
			</#list>
	    )
	</insert>
	
	<!-- 批量插入 -->
	<insert id="batchInsert${className_d}" parameterType="java.util.List" useGeneratedKeys="true">
		 <selectKey resultType="long" keyProperty="${primaryKeyCode}" order="AFTER">  
	        SELECT  LAST_INSERT_ID()  
	    </selectKey>  
	    insert into ${className}
	    (
	    ${primaryKeyCode}
			<#list tableCarrays as tableCarrays>
				<#if tableCarrays.carrayName_x != primaryKeyCode>
					<if test="${tableCarrays.carrayName_x} != null" >
				    	, ${tableCarrays.carrayName_x}
				    </if>
				</#if>
			</#list>
	    )
	    values
	    (
	    	<foreach collection="list" item="item" index="index" separator="," >
		    	<#list tableCarrays as tableCarrays>
					<#if tableCarrays.carrayName_x != primaryKeyCode>
						<if test="${tableCarrays.carrayName_x} != null" >
					    	, ${r'#{'}${tableCarrays.carrayName_x}${r'}'}
					    </if>
					</#if>
				</#list>  
	    	</foreach>  
	    )
	    
	</insert>
	
	<!-- 删除数据 -->
	<delete id="delete${className_d}" parameterType="String">
		delete from ${className} where ${primaryKeyCode}=  ${r'#{'}key${r'}'};
	</delete>
	
	<!-- 删除所有数据 -->
	<delete id="deleteAll">
		delete from ${className}
	</delete>
	
	<!-- 条件删除数据 -->
	<delete id="delete${className_d}ByCondition" parameterType="${className_d}">
		delete from ${className} 
		<where>
			<if test="${primaryKeyCode} != null">
		 		${primaryKeyCode} = ${r'#{'}${primaryKeyCode}${r'}'}
		 	</if>
		 	<include refid="comListQuery${className_d}"/>
			<if test="otherCondStr != null">
				and ${r'#{'}otherCondStr${r'}'}
			</if>
		</where>
	</delete>
	
	
	<!-- 修改数据 -->
	<update id="modify${className_d}" parameterType="${className_d}">
    	update ${className} set ${primaryKeyCode} = ${r'#{'}${primaryKeyCode}${r'}'}
	    <#list tableCarrays as tableCarrays>
			<#if tableCarrays.carrayName_x != primaryKeyCode>
				<if test="${tableCarrays.carrayName_x} != null">
					<choose>
					    <when test="${tableCarrays.carrayName_x} != ''">
					    	, ${tableCarrays.carrayName_x} = ${r'#{'}${tableCarrays.carrayName_x}${r'}'}
					    </when>
					    <otherwise>
					    	, ${tableCarrays.carrayName_x} = DEFAULT
					    </otherwise>
					</choose>
				</if>
			</#if>
		</#list>
		<where>
			${primaryKeyCode} = ${r'#{'}${primaryKeyCode}${r'}'};
		</where>
	</update>
</mapper>